package com.xgg.taiwuinfo.fragement;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.xgg.taiwuinfo.MainActivity;
import com.xgg.taiwuinfo.R;
import com.xgg.taiwuinfo.adapter.GongfaAdapter;
import com.xgg.taiwuinfo.filter.Filter;
import com.xgg.taiwuinfo.filter.FilterComponent;
import com.xgg.taiwuinfo.filter.FilterList;
import com.xgg.taiwuinfo.gongfa.base.Gongfa;
import com.xgg.taiwuinfo.gongfa.base.GongfaList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GongfaListFragement extends Fragment implements AdapterView.OnItemClickListener , AdapterView.OnItemSelectedListener{
    public List<Map<String, String>> mGongfaDataList = new ArrayList<>();
    ListView list_gongfa;
    TextView txt_desc , txt_count;
    GongfaAdapter mAdapter;
    Spinner spin_filter;
    Filter mCurFilter;
    String[] spinnerItems = new String[]{};
    ArrayAdapter<String> spinnerAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragement_gongfalist, container, false);
    }

    @Override
    public void onStart()
    {
        super.onStart();
        list_gongfa = getView().findViewById(R.id.list_gongfa);
        list_gongfa.setOnItemClickListener(this);
        spin_filter = getView().findViewById(R.id.spin_filter);
        spin_filter.setOnItemSelectedListener(this);
        mAdapter = new GongfaAdapter(getActivity() , mGongfaDataList , 0 ,null,null);
        list_gongfa.setAdapter(mAdapter);
        txt_desc = getView().findViewById(R.id.txt_desc);
        txt_count = getView().findViewById(R.id.txt_count);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        ReloadFilter();
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
    {
        if (mGongfaDataList.size() < position + 1)
        {
            //下标越界
            return;
        }
        Map<String, String> map = mGongfaDataList.get(position);
        String id = map.get("id");//这是总表中的序号
        MainActivity act = (MainActivity)getActivity();
        act.NavToGongfaDetail(id);
    }

    public void ReloadFilter()
    {
        int pos = 0;
        int length = FilterList.getList().size();
        spinnerItems = new String[length];
        for (int i = 0; i < FilterList.getList().size(); i++) {
            Filter ft = FilterList.getList().get(i);
            spinnerItems[i] = ft.mName;
            if (mCurFilter == ft) {
                pos = i;
            }
        }
        //简单的string数组适配器：样式res，数组
        spinnerAdapter = new ArrayAdapter<>(getActivity(),
                android.R.layout.simple_spinner_item , spinnerItems);
        //下拉的样式res
        spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //绑定 Adapter到控件
        spin_filter.setAdapter(spinnerAdapter);
        //注意下mcurfilter还在不在现列表里，如果在的话
        spin_filter.setSelection(pos);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapter,View view,int position,long id) {
        //获取选择的项的值
        mCurFilter = FilterList.getList().get(position);
        MainActivity act = (MainActivity)getActivity();
        act.SetCurFilterId(position+"");
        FilterAndSort();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mCurFilter.mList.size(); i++)
        {
            sb.append(mCurFilter.mList.get(i).getDesc());
        }
        txt_desc.setText(sb.toString());
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {}

    void FilterAndSort()
    {
        if (mGongfaDataList != null)
        {
            mGongfaDataList.clear();
        }
        else
        {
            mGongfaDataList = new ArrayList<>();
        }

        for (int i = 0; i < GongfaList.getList().size(); i++)
        {
            Gongfa gf = GongfaList.getList().get(i);
            boolean show = true;
            String tip;
            String showtip = "";
            for (int j = 0 ; j < mCurFilter.mList.size() ; j++)
            {
                FilterComponent fc = mCurFilter.mList.get(j);
                tip = fc.FilterGongfa(gf);
                if (tip == null)
                {
                    //有一条不合就直接看下个功法
                    show = false;
                    break;
                }
                if (showtip.equals(""))
                    showtip = tip;
            }
            if (show)
            {
                Map<String, String> map = new HashMap<>();
                map.put("id" , gf.getId() + "");
                map.put("name", gf.mName);
                //map.put("id", it.m_id);
                map.put("type", gf.getTypeString(getActivity()));
                map.put("level", gf.getLevelColor() + "");
                map.put("element" , gf.getElementString(getActivity()));
                map.put("elementcolor" , gf.getElementColor() + "");
                map.put("inherit" , gf.getInheritString() );
                map.put("tip" , showtip);
                mGongfaDataList.add(map);
            }
        }

        //过滤结束后排序
        //排序结束后刷新
        mAdapter.notifyDataSetChanged();
        txt_count.setText(String.format("%d / %d / 756" , mGongfaDataList.size() , GongfaList.getList().size()));
    }
}
